﻿#ifndef RADIATEDTESTFLOWSESSION_H
#define RADIATEDTESTFLOWSESSION_H

#include "itestflow.h"
#include <QThread>
#include "3rdparty/includes/signalgenerators/signalgenerator/isignalgenerator.h"
#include "3rdparty/includes/poweramplifiers/poweramplifier/ipoweramplifier.h"
#include "3rdparty/includes/powermeters/powermeter/ipowermeter.h"
#include "3rdparty/includes/fieldprobes/fieldprobe/ifieldprobe.h"
#include "3rdparty/includes/switchunits/switchunit/iswitchunit.h"
#include "3rdparty/includes/receivers/receiver/ireceiver.h"
#include "model/deviceinstancemanager.h"
#include "3rdparty/includes/deviceio/ideviceio.h"

// 辐射法流程的会话对象
struct RadiatedTestFlowSession : public TestFlowSession {
    DeviceHandle<IDeviceIO> generatorIO;
    DeviceHandle<IDeviceIO> amplifierIO;
    DeviceHandle<IDeviceIO> forwardPowerMeterIO;
    DeviceHandle<IDeviceIO> reversePowerMeterIO;
    DeviceHandle<IDeviceIO> monitorPowerMeterIO;
    DeviceHandle<IDeviceIO> receiverIO;
    DeviceHandle<IDeviceIO> fieldProbeIO;
    DeviceHandle<IDeviceIO> switchUnitIO;

    DeviceHandle<ISignalGenerator> generator;
    DeviceHandle<IPowerAmplifier> amplifier;
    DeviceHandle<IPowerMeter> forwardPowerMeter;
    DeviceHandle<IPowerMeter> reversePowerMeter;
    DeviceHandle<IPowerMeter> monitorPowerMeter;
    DeviceHandle<IFieldProbe> fieldProbe;
    DeviceHandle<IReceiver> receiver;
    DeviceHandle<ISwitchUnit> switchUnit;
    int fwdPowerMeterChannel = 1;
    int revPowerMeterChannel = 1;
    int monPowerMeterChannel = 1;

    bool rfMuted = true; //设置静默

    void safeMuteAllOutputs() override;
    bool ensureOutputsEnabled() override;
    bool initReceiver(const QString &mode = "N9038A");
    bool setAmplifierOutput(bool st);
    bool setGeneratorOutput(bool st);

    bool setFrequency(double freq, const TestFlowCallbacks &callbacks);
    bool setInputLevel(double level, const TestFlowCallbacks &callbacks);

    double readFwdPower(const QString &arg = "");
    double readRevPower(const QString &arg = "");
    double readMonitorPower(const QString &arg = "");
    double readFieldPower();

    void closeAll() override;
};

// 辐射法流程策略实现
class RadiatedFlow : public ITestFlow
{
public:
    QString name() const override { return "Radiated"; }
    bool ensureSessionsForSubrange(const Exec::Subrange &subrange,
                                   TestFlowSessionPtr reuseCandidate,
                                   TestFlowSessionPtr &outSession,
                                   TestFlowCallbacks callbacks,
                                   QString &errorMessage) override;

    void safeMuteAllOutputs(const TestFlowSessionPtr &session) override;

    bool measureOnePoint(const Exec::Subrange &subrange,
                         int frequencyIndex,
                         const TestFlowSessionPtr &session,
                         TestFlowCallbacks callbacks,
                         QString &errorMessage) override;
    void setPlanOptions(const FlowPlanOptions &opts) override;

private:
    bool checkConfigurationValidity(const Exec::Subrange &subrange, QString &errorMessage);
    int adjust(double &currentInputVal, double currentVal, double targetVal, double precisionGradeUp, double precisionGradeDown, double levelMin, double levelMax, double inputMax, QString &errorMessage);

private:
    // 保存计划级参数
    FlowPlanOptions options_;
    //监测设备是否为场强探头，用于区分监测设备为场强探头或功率设备如频谱仪或功率计等
    bool monitorDeviceIsFieldProbes = false;
    //保存上一个频率测试时信号源幅度
    double preInputVal = -95;
};

#endif // RADIATEDTESTFLOWSESSION_H
